package com.lufei.message_system.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lufei.message_system.Data.MbtiTypeDescription;
import com.lufei.message_system.Data.MbtiTypeDescriptionVD.*;
import com.lufei.message_system.Data.PageResult;
import com.lufei.message_system.Data.Result;
import com.lufei.message_system.Mapper.MbtiTypeDescriptionMapper;
import com.lufei.message_system.Service.MbtiTypeDescriptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MbtiTypeDescriptionServiceImpl implements MbtiTypeDescriptionService {

    @Autowired
    private MbtiTypeDescriptionMapper mapper;

    @Override
    public Result createMbtiTypeDescription(MbtiTypeDescriptionCreateDTO createDTO) {
        MbtiTypeDescription entity = new MbtiTypeDescription();
        BeanUtils.copyProperties(createDTO, entity);
        int result = mapper.insert(entity);
        return result > 0 ? Result.success("创建成功") : Result.error("创建失败");
    }

    @Override
    public Result deleteMbtiTypeDescription(String mbtiType) {
        int result = mapper.deleteById(mbtiType);
        return result > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Override
    public Result updateMbtiTypeDescription(String mbtiType, MbtiTypeDescriptionUpdateDTO updateDTO) {
        MbtiTypeDescription entity = mapper.selectById(mbtiType);
        if (entity == null) {
            return Result.error("MBTI类型不存在");
        }
        BeanUtils.copyProperties(updateDTO, entity);
        // 设置更新时间为当前时间
        entity.setUpdateTime(new Date());
        int result = mapper.updateById(entity);
        return result > 0 ? Result.success("更新成功") : Result.error("更新失败");
    }

    @Override
    public Result<MbtiTypeDescriptionVO> getMbtiTypeDescription(String mbtiType) {
        MbtiTypeDescription entity = mapper.selectById(mbtiType);
        if (entity == null) {
            return Result.error("数据不存在");
        }
        MbtiTypeDescriptionVO vo = new MbtiTypeDescriptionVO();
        BeanUtils.copyProperties(entity, vo);
        return Result.success(vo);
    }

    @Override
    public Result<PageResult<MbtiTypeDescriptionVO>> listMbtiTypeDescriptions(Integer page, Integer size, MbtiTypeDescriptionQueryDTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<MbtiTypeDescription> wrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (queryDTO != null) {
            if (StringUtils.hasText(queryDTO.getMbtiType())) {
                wrapper.like(MbtiTypeDescription::getMbtiType, queryDTO.getMbtiType());
            }
            if (StringUtils.hasText(queryDTO.getTypeName())) {
                wrapper.like(MbtiTypeDescription::getTypeName, queryDTO.getTypeName());
            }
        }

        // 创建分页对象，确保页码从1开始
        page = Math.max(1, page);
        size = Math.max(1, Math.min(size, 50)); // 限制每页最大条数为50
        Page<MbtiTypeDescription> pageInfo = new Page<>(page, size);
        
        // 执行分页查询
        Page<MbtiTypeDescription> resultPage = mapper.selectPage(pageInfo, wrapper);

        // 转换为VO对象，并确保返回的数据不超过页面大小
        List<MbtiTypeDescriptionVO> vos = resultPage.getRecords().stream()
                .limit(size)
                .map(entity -> {
                    MbtiTypeDescriptionVO vo = new MbtiTypeDescriptionVO();
                    BeanUtils.copyProperties(entity, vo);
                    return vo;
                })
                .collect(Collectors.toList());

        // 构建分页结果
        PageResult<MbtiTypeDescriptionVO> pageResult = new PageResult<>();
        pageResult.setList(vos);
        pageResult.setTotal(resultPage.getTotal());
        pageResult.setPage(page);
        pageResult.setSize(size);

        return Result.success(pageResult);
    }
}